using System;
using System.Collections;
using System.Xml;

namespace AleaClasses
{
	/// <summary>
	/// TEdges is a collection of TEdge
	/// </summary>
	public class TEdges : CollectionBase, ITrackEdits  
	{
		private TEdgesTrackEdits fEdgesTrackEdits;

		private void Constructor()
		{
			fEdgesTrackEdits = new TEdgesTrackEdits(this);
		}

		public TEdges(): base()
		{
			Constructor();
		}

		public TEdges(XmlElement aEdgesElem, TNodes aNodes, TLabels aLabels): base()
		{
			Constructor();

			foreach(XmlElement lEdgeElem in aEdgesElem)
			{
				TEdge lEdge = new TEdge(lEdgeElem, aNodes, aLabels);
				this.Add(lEdge);
			}
		}

		public XmlElement SaveToXml(XmlDocument aDoc)
		{
			XmlElement retval = null;
			XmlElement elem = null;

			retval = aDoc.CreateElement("Edges");

			foreach(TEdge lEdge in this)
			{
				elem = lEdge.SaveToXml(aDoc);
				if (elem != null)
					retval.AppendChild(elem);
			}

			return retval;
		}

		public TEdge this[ int index ]  
		{
			get  
			{
				return( (TEdge) List[index] );
			}
			set  
			{
				if (List[index] != value)
				{
					List[index] = value;
					fEdgesTrackEdits.DoSetNeedsSave();
				}	
			}
		}

		public int Add( TEdge value )  
		{
			int retval = List.Add( value );
			fEdgesTrackEdits.DoSetNeedsSave();
			return( retval );
		}

		public int IndexOf( TEdge value )  
		{
			return( List.IndexOf( value ) );
		}

		public void Insert( int index, TEdge value )  
		{
			List.Insert( index, value );
			fEdgesTrackEdits.DoSetNeedsSave();
		}

		public void Remove( TEdge value )  
		{
			List.Remove( value );
			fEdgesTrackEdits.DoSetNeedsSave();
		}

		public bool Contains( TEdge value )  
		{
			// If value is not of type TEdge, this will return false.
			return( List.Contains( value ) );
		}

		protected override void OnInsert( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("AleaClasses.TEdge") )
				throw new ArgumentException( "value must be of type TEdge.", "value" );
		}

		protected override void OnRemove( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("AleaClasses.TEdge") )
				throw new ArgumentException( "value must be of type TEdge.", "value" );
		}

		protected override void OnSet( int index, Object oldValue, Object newValue )  
		{
			if ( newValue.GetType() != Type.GetType("AleaClasses.TEdge") )
				throw new ArgumentException( "newValue must be of type TEdge.", "newValue" );
		}

		protected override void OnValidate( Object value )  
		{
			if ( value.GetType() != Type.GetType("AleaClasses.TEdge") )
				throw new ArgumentException( "value must be of type TEdge." );
		}
		#region ITrackEdits Members

		public bool NeedsSave()
		{
			return fEdgesTrackEdits.NeedsSave();
		}

		public void ClearNeedsSave()
		{
			fEdgesTrackEdits.ClearNeedsSave();
		}

		#endregion
	}

	public class TEdgesTrackEdits: TTrackEdits
	{
		private TEdges fEdges;

		public TEdgesTrackEdits(TEdges aEdges)
		{
			fEdges = aEdges;
		}

		protected override void ClearChildrenNeedsSave()
		{
			foreach (ITrackEdits te in fEdges)
			{
				te.ClearNeedsSave();
			}
		}

		protected override bool ChildrenNeedSave()
		{
			bool retval = false;

			foreach (ITrackEdits te in fEdges)
			{
				retval |= te.NeedsSave();
			}

			return retval;
		}
		
		internal void DoSetNeedsSave()
		{
			this.SetNeedsSave();
		}
	}
}
